/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.app;

import java.util.*;





import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.qbooks.app.AppDAO;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class InitAppCommand implements Command
{
	private final String D = "\"";

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	String os	= (String)request.getAttribute("os");
    	String appVersion = (String)request.getParameter("app_version");
    	String appId = (String)request.getParameter("app_id");

    	int result_code = 100;
    	
    	HashMap infos = new HashMap();
    	infos.put("os_type", os);

    	
    	infos.put("app_id", appId);

    	AppDAO dao 		= new AppDAO();
    	HashList list	= dao.selectAppVersion(infos);


		if ("android".equals(os)) {
			JSONObject json = new JSONObject();
	    	json.put("if_code", "INT");
	    	json.put("result_code", result_code);
	    	json.put("last_version", list.get("last_version",0));
	    	json.put("update_url", list.get("update_url",0));
	    	json.put("qbooks_yn", list.get("qbooks_yn",0));
	    	json.put("qbooks_url", list.get("qbooks_url",0));
	    	json.put("ad_code", list.get("ad_code",0));
	    	json.put("notice_yn", list.get("notice_yn",0));
	    	json.put("notice", list.get("notice",0));
	    	json.put("notice_url", list.get("notice_url",0));
	    	request.setAttribute("jsonData", json.toString());
		}
		else if ("ios".equals(os)) {
			StringBuffer jsonData = new StringBuffer();
			jsonData.append("[")
					.append(D).append(list.get("last_version",0)).append(D)
					.append(", ").append(D).append(list.get("update_url",0)).append(D)
					.append(", ").append(D).append(list.get("qbooks_yn",0)).append(D)
					.append(", ").append(D).append(list.get("qbooks_url",0)).append(D)
					.append(", ").append(D).append(list.get("ad_code",0)).append(D)
					.append(", ").append(D).append(list.get("notice_yn",0)).append(D)
					.append(", ").append(D).append(list.get("notice",0)).append(D)
					.append(", ").append(D).append(list.get("notice_url",0)).append(D)
					.append(", ").append(D).append("n").append(D)
					.append(", ").append(D).append("n").append(D)
					.append("]");
			
			

			request.setAttribute("jsonData", jsonData.toString());
		}

		return "/common/json_view.jsp";

    }
}
